﻿@namespace MudBlazor
@typeparam T
@inherits MudBaseInput<T>

<CascadingValue Name="Standalone" Value="false" IsFixed="true">
	<div class="mud-select" id="@_elementId">
		<MudInputControl Label="@Label" Variant="@Variant" HelperText="@HelperText" HelperTextOnFocus="@HelperTextOnFocus" FullWidth="@FullWidth" Margin="@Margin" Class="@Classname" Style="@Style"
						 Error="@Error" ErrorText="@ErrorText" Disabled="@Disabled" @onclick="@ToggleMenu" Required="@Required">
			<InputContent>
                <MudInput @ref="_elementReference" InputType="@(CanRenderValue || (Strict && !IsValueInList) ? InputType.Hidden : InputType.Text)"
                          Class="mud-select-input" Margin="@Margin" Placeholder="@Placeholder"
                          Variant="@Variant"
                          TextUpdateSuppression="false"
                          Value="@(Strict && !IsValueInList ? null : Text)" DisableUnderLine="@DisableUnderLine"
                          Disabled="@Disabled" ReadOnly="true" Error="@Error"
                          OnAdornmentClick="@OnAdornmentClick" AdornmentIcon="@_currentIcon" Adornment="@Adornment"
                          AdornmentColor="@AdornmentColor" IconSize="@IconSize" AdornmentText="@AdornmentText"
                          Clearable="@Clearable" OnClearButtonClick="(async (e) => await SelectClearButtonClickHandlerAsync(e))"
                          @attributes="UserAttributes" OnBlur="@OnLostFocus">
                    @if (CanRenderValue)
                    {
                        @GetSelectedValuePresenter()
                    }
                </MudInput>
				<MudPopover Open=@(_isOpen) MaxHeight="@MaxHeight" AnchorOrigin="@AnchorOrigin" TransformOrigin="@TransformOrigin" Class="@PopoverClass" RelativeWidth="true">
					<CascadingValue Value="@((IMudSelect)this)" IsFixed="true">
						<MudList Clickable="true" Dense="@Dense" @bind-SelectedValue="_activeItemId">
                            @if (MultiSelection && SelectAll)
							{
								<MudListItem Icon="@SelectAllCheckBoxIcon" Text="@SelectAllText" OnClick="SelectAllClickAsync" Dense="@Dense" Class="mb-2" />
                                <MudDivider />
							}
							@ChildContent
						</MudList>
					</CascadingValue>
				</MudPopover>
			</InputContent>
		</MudInputControl>
	</div>
    @*Shadow select items for IsValueInList and CanRenderValue*@
    <CascadingValue Value="@((IMudShadowSelect)this)" IsFixed="true">
        <CascadingValue Name="HideContent" Value="true">
			@ChildContent
		</CascadingValue>
	</CascadingValue>

</CascadingValue>
<!-- mousedown instead of click needed to close the menu before OnLostFocus runs -->
<MudOverlay Visible="_isOpen" @onmousedown="@(() => CloseMenu(false))" @ontouchstart="@(() => CloseMenu(false))" LockScroll="@LockScroll" />